from module.dynamic_module.k_chart.model.console_model import ConsoleModel
from module.dynamic_module.k_chart.view.super_view import SuperView
from module.dynamic_module.k_chart.model.ele_model import KLinesModel, VolumeModel, FactorModel
from conf import conf
from queue import Queue


class SuperModel:
    """顶级窗口模型类"""
    def __init__(self):
        self.q: Queue = None
        self.symbol_name = None
        
        self.draw_data_k = None
        self.draw_data_vol = None
        self.draw_data_factor_1 = None
        self.draw_data_factor_2 = None

        self.super_view_size = conf.KChart.super_view_size.value
        self.prev_super_view_size = conf.KChart.super_view_size.value
        self.console_view_size = None
        self.k_lines_view_size = None
        self.volume_view_size = None
        self.factor_view_size = None

        self.factor_model_num = 2

        self.console_model = None

        self.k_lines_model = None
        self.volume_model = None
        self.factor_model_ls = None
        self.super_view = None

        self.signal_ls = []

    def sec_init(self):

        self.console_view_size = [conf.KChart.console_view_width.value, self.super_view_size[1]]

        k_lines_view_weight = conf.KChart.ele_weight.value[0]
        volume_view_weight = conf.KChart.ele_weight.value[1]
        factor_view_weight = conf.KChart.ele_weight.value[2] * self.factor_model_num

        per_vertical_size = int(self.super_view_size[1] / (k_lines_view_weight + volume_view_weight + factor_view_weight))-2
        self.k_lines_view_size = [self.super_view_size[0] - self.console_view_size[0], per_vertical_size*k_lines_view_weight]
        self.volume_view_size = [self.super_view_size[0] - self.console_view_size[0], per_vertical_size*volume_view_weight]
        self.factor_view_size = [self.super_view_size[0] - self.console_view_size[0], per_vertical_size*conf.KChart.ele_weight.value[2]]

    def set_symbol(self, symbol_name):
        self.symbol_name = symbol_name

    def set_q(self, q: Queue):
        self.q = q

    def work(self):
        # 获取第一次的数据
        draw_data_dc = self.q.get()
        self.draw_data_k = draw_data_dc["draw_data_k"]
        self.draw_data_vol = draw_data_dc["draw_data_vol"]
        self.draw_data_factor_1 = draw_data_dc["draw_data_factor_1"]
        self.draw_data_factor_2 = draw_data_dc["draw_data_factor_2"]
        # 初始化
        self.model_instantiate()
        self.view_instantiate()
        # 执行相关方法
        self.model_mobilize()
        self.view_mobilize()

        # 创建线程，循环获取q中的数据，并更新至类属性
        # thread = Thread(target=self.work_thread)
        # thread.start()
        self.work_thread()

    def work_thread(self):
        while True:
            draw_data_dc = self.q.get()
            if draw_data_dc is None:
                print("K线图获得None值，跳出数据检测并执行窗口销毁操作。")
                break
            self.draw_data_k = draw_data_dc["draw_data_k"]
            self.draw_data_vol = draw_data_dc["draw_data_vol"]
            self.draw_data_factor_1 = draw_data_dc["draw_data_factor_1"]
            self.draw_data_factor_2 = draw_data_dc["draw_data_factor_2"]
            # 有数据更新
            self.refresh()
        # 关闭窗口
        self.super_view.root.destroy()
        # 执行模块关闭操作

    def model_instantiate(self):
        # 实例化Console窗口实体类
        self.console_model = ConsoleModel(self)
        # 实例化K线窗口实体类
        self.k_lines_model = KLinesModel(self, self.k_lines_view_size, show_x_axis=True, show_legend=True)
        # 实例化Volume窗口实体类
        self.volume_model = VolumeModel(self, self.volume_view_size, show_x_axis=False, show_legend=True)
        # 实例化Factor窗口实体类
        self.factor_model_ls = []
        for i in range(self.factor_model_num):
            factor_model = FactorModel(self, self.factor_view_size, show_x_axis=False, show_legend=True)
            self.factor_model_ls.append(factor_model)

    def view_instantiate(self):
        # 实例化顶级窗口视图类
        self.super_view = SuperView(self)
        # 实例化ele窗口及console窗口视图类
        self.super_view.view_instantiate()

    def model_mobilize(self):
        # 补充执行model类实例化后需执行的方法
        # 执行窗口实体类下的相关方法
        self.console_model_mobilize()
        self.ele_model_mobilize()

    def console_model_mobilize(self):
        self.console_model.sec_init()
        # Console窗口
        # 待ele窗口实体类实例化后，根据实际绘图窗口的大小，获取由console管理的k_max_num变量
        self.console_model.change_index_k_cont_width()

    def ele_model_mobilize(self):
        # K线图
        self.k_lines_model.sec_deal(self.console_model.draw_data_k_section)
        self.k_lines_model.k_px_info()

        # Volume图
        self.volume_model.sec_deal(self.console_model.draw_data_vol_section)
        self.volume_model.volume_px_info()

        # Factor图
        for i, factor_model in enumerate(self.factor_model_ls):
            if i == 0:
                factor_model.sec_deal(self.console_model.draw_data_factor_1_section)
            elif i == 1:
                factor_model.sec_deal(self.console_model.draw_data_factor_2_section)
            else:
                pass

    def view_mobilize(self):
        # 执行窗口视图类下的相关方法
        self.super_view.view_mobilize()

    def view_update(self):
        # 执行窗口视图类下的相关方法
        self.super_view.view_update()
        # 根据时间获得消息的内容，并展示至相应entry
        self.console_model.show_message()

    def reset(self):
        self.console_model.change_index_k_cont_width(conf.KChart.index.value, conf.KChart.k_cont_width.value)
        # 重置界面相关参数为默认值，并刷新界面
        self.model_mobilize()
        # 重置  k_cont_width、index、为初始值
        self.view_update()

    def refresh(self):
        # 刷新界面，以此实现实时数据兼容和行为事件。
        self.model_mobilize()  # 从super model处获得更新后的数据
        self.view_update()

    def mainloop(self):
        self.super_view.mainloop()

    def time2index(self, time_val):
        index = -len(self.draw_data_k) + self.draw_data_k.index.tolist().index(time_val)
        return index

    def index2time(self, index):
        time_val = self.draw_data_k.index.tolist()[index]
        return time_val


















